#include "EditPatientInfo.h"

#include <QScrollBar>
namespace UserManagementSystem {

EditPatientInfo::EditPatientInfo(QWidget* parent,UserRepository* m_userrepository,int row)
    :BaseDialogWithShadow(parent)
{
    qDebug()<<__FUNCTION__;

    //将数据加载出来
    if(m_userrepository==nullptr){
        qDebug()<<"userrepository为空";
    }else{
        qDebug()<<"userrepository不为空:"<<row<<m_userrepository->GetPatientData().size();
        m_data=m_userrepository->GetPatientData().at(row);
    }



    //获取内容函数为工作窗口
    if(this->GetContentWidget()){
        m_workWidget=static_cast<BaseDialogWithShadow*>(this)->GetContentWidget();
    }
    m_workWidget->setMinimumSize(300,500);

    //新建一个内容窗口放所有的label,放入scroll中
    m_contentWidget=new QFrame();
    m_workLayout=new QVBoxLayout(m_workWidget->GetContentWindow());


    //滚动窗口区域作为滚动条的挂在窗口
    m_scrollArea=new QScrollArea(m_workWidget->GetContentWindow());
    m_scrollArea->setAlignment(Qt::AlignRight);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setWidget(m_contentWidget);
    m_scrollArea->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

    m_mainLayout=new QVBoxLayout(m_contentWidget);
    m_btnsLayout=new QHBoxLayout();

    m_nameLab=new QLabel();
    m_nameEdit=new QLineEdit();

    m_firstNameLab=new QLabel();
    m_firstNameEdit=new QLineEdit();

    m_genderLab=new QLabel();

    m_birthdayLab=new QLabel();
    m_birbox=new CalendarComboBoxLikeWidget();

    m_phoneLab=new QLabel();
    m_phoneEdit=new QLineEdit();

    m_mailLab=new QLabel();
    m_mailEdit=new QLineEdit();

    m_addressLab=new QLabel();
    m_addressEdit=new QLineEdit();

    m_annotationLab=new QLabel();
    m_annotationEdit=new QPlainTextEdit();


    m_btnsGroup=new QButtonGroup();
    m_manBtn=new QRadioButton();
    m_femaleBtn=new QRadioButton();
    m_otherBtn=new QRadioButton();
    SetDefaultLayout();

    connect(this->m_workWidget,&BaseDialog::comfirm_signal,this,&EditPatientInfo::On_comfirm_Btn_Clicked);
    connect(m_manBtn,&QRadioButton::clicked,this,&EditPatientInfo::On_man_clicked);
    connect(m_femaleBtn,&QRadioButton::clicked,this,&EditPatientInfo::On_female_clicked);
    connect(m_otherBtn,&QRadioButton::clicked,this,&EditPatientInfo::On_other_clicked);

    LoadDataToWidget();
}

void EditPatientInfo::SetDefaultLayout()
{
    m_btnsLayout->setAlignment(Qt::AlignLeft);
    m_btnsLayout->addWidget(m_manBtn);
    m_btnsLayout->addWidget(m_femaleBtn);
    m_btnsLayout->addWidget(m_otherBtn);

    m_workLayout->addWidget(m_scrollArea,1);

    m_mainLayout->setAlignment(Qt::AlignLeft);
    m_mainLayout->addWidget(m_nameLab);
    m_mainLayout->addWidget(m_nameEdit);
    m_mainLayout->addWidget(m_firstNameLab);
    m_mainLayout->addWidget(m_firstNameEdit);

    m_mainLayout->addWidget(m_genderLab);
    m_mainLayout->addLayout(m_btnsLayout);


    m_mainLayout->addWidget(m_birthdayLab);
    m_mainLayout->addWidget(m_birbox);
    m_mainLayout->addWidget(m_phoneLab);
    m_mainLayout->addWidget(m_phoneEdit);
    m_mainLayout->addWidget(m_mailLab);
    m_mainLayout->addWidget(m_mailEdit);
    m_mainLayout->addWidget(m_addressLab);
    m_mainLayout->addWidget(m_addressEdit);
    m_mainLayout->addWidget(m_annotationLab);
    m_mainLayout->addWidget(m_annotationEdit);

    m_btnsGroup->addButton(m_manBtn);
    m_btnsGroup->addButton(m_femaleBtn);
    m_btnsGroup->addButton((m_otherBtn));

    m_contentWidget->adjustSize();

    SetDefaultStyle();

}

void EditPatientInfo::SetDefaultStyle()
{


    QString label_sty=(R"(
        QLabel {
        border: none;
        color: black;
        font-size:12px;
        font-weight:bold;
        padding: 0px;
    })");


    m_nameLab->setText(tr("名字*"));
    m_nameLab->setStyleSheet(label_sty);

    m_firstNameLab->setText(tr("姓*"));
    m_firstNameLab->setStyleSheet(label_sty);

    m_genderLab->setText(tr("性别*"));
    m_genderLab->setStyleSheet(label_sty);

    m_birthdayLab->setText(tr("生日*"));
    m_birthdayLab->setStyleSheet(label_sty);

    m_phoneLab->setText(tr("电话*"));
    m_phoneLab->setStyleSheet(label_sty);

    m_mailLab->setText(tr("邮箱*"));
    m_mailLab->setStyleSheet(label_sty);

    m_addressLab->setText(tr("地址*"));
    m_addressLab->setStyleSheet(label_sty);

    m_annotationLab->setText(tr("备注*"));
    m_annotationLab->setStyleSheet(label_sty);

    //lineedit的样式
    QString edit_sty=(R"(
    QLineEdit {
            border: none;              /* 无边框 */
            background-color: rgb(185,205,229); /* 浅蓝色背景 */
            padding: 4px;               /* 给文字留点内边距，看起来更舒服 */
            border-radius: 2px;
        }
    )");


    m_nameEdit->setStyleSheet(edit_sty);
    m_firstNameEdit->setStyleSheet(edit_sty);
    m_birbox->setStyleSheet("border: none;background-color: rgb(185,205,229);padding: 0px;border-radius: 2px;");

    m_phoneEdit->setStyleSheet(edit_sty);
    m_mailEdit->setStyleSheet(edit_sty);
    m_addressEdit->setStyleSheet(edit_sty);
    m_annotationEdit->setStyleSheet("border: none;background-color: rgb(185,205,229);padding: 0px;");

    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    //滚动条样式
    QString scroll_sty=(R"(
    QScrollArea  QScrollBar:vertical {
        background: transparent;
        width: 4px;
        margin: 0px 0px 0px 0px;
    }

    QScrollArea  QScrollBar::handle:vertical {
        background: gray;
        height: 6px;
        width: 4px;
        border-radius: 2px;
    }

    QScrollArea QScrollBar::sub-line, QScrollArea  QScrollBar::add-line,
    QScrollArea QScrollBar::up-arrow, QScrollArea  QScrollBar::down-arrow:vertical {
        height: 0px;
    }
)");


    qDebug() << "m_contentWidget height:" << m_contentWidget->sizeHint().height();
    qDebug() << "scrollArea viewport height:" << m_scrollArea->viewport()->height();


    m_scrollArea->verticalScrollBar()->setStyleSheet(scroll_sty);

    //按钮
    m_manBtn->setText(tr("男"));
    m_femaleBtn->setText(tr("女"));
    m_otherBtn->setText(tr("其他"));

}

void EditPatientInfo::LoadDataToWidget()
{
    //打印data里的数据
    m_data.PrintPatientData();

    m_nameEdit->setText(m_data.name);

    m_firstNameEdit->setText(m_data.first_name);

    m_birbox->GetBirthLabel()->setText(m_data.birthday);

    m_phoneEdit->setText(m_data.phone);

    m_mailEdit->setText(m_data.email);

    m_addressEdit->setText(m_data.address);

    m_annotationEdit->setPlainText(m_data.annotation);

}

void EditPatientInfo::On_comfirm_Btn_Clicked()
{
    //获得键入的各个属性
    QString name=m_nameEdit->text();
    QString first_name=m_firstNameEdit->text();

    QString phone=m_phoneEdit->text();
    QString mail=m_mailEdit->text();
    QString address=m_addressEdit->text();
    QString annotation=m_annotationEdit->toPlainText();

    qDebug()<<"确认患者编辑后的信息："<<name<<" "<<first_name<<" "<<phone<<" "<<mail<<" "<<address<<" "<<annotation;
    qDebug()<<"性别："<<m_gender;

}

void EditPatientInfo::On_man_clicked()
{
    m_gender=0;
}

void EditPatientInfo::On_female_clicked()
{
    m_gender=1;
}

void EditPatientInfo::On_other_clicked()
{
    m_gender=2;
}

}
